/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-04-28 11:24:35
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2023-09-11 18:12:25
 * @FilePath: \greenbeeadmin\controllers\admin\admin.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package controllers

import (
	"fmt"
	adminService "greenbeeadmin/controllers/admin/service"
	greenbee "greenbeeadmin/controllers/greenbee"
	greenbeeUtils "greenbeeadmin/controllers/greenbee/utils"
	"greenbeeadmin/models"
	"net/url"
	"strconv"

	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
)

// 后台首页
type AdminController struct {
	greenbee.AdminBaseController
}

func (c *AdminController) Get() {
	//查询管理员信息
	user := c.GetSession("user")
	id := 0
	if user != nil {
		AdminUser := user.(models.AdminUser)
		id = AdminUser.Id
		// cookie 更新 自动续期时间 +1天
		admin_id_str := strconv.Itoa(id)
		system_key, _ := beego.AppConfig.String("greenbee_system_key") //读取配置 加密key
		p := []byte(admin_id_str)                                      //加密数据
		key := []byte(system_key)                                      //加密key
		// AES CBC mode encryption
		ciphertext, _ := greenbeeUtils.Base64AESCBCEncrypt(p, key)
		cooikes := url.QueryEscape(ciphertext)
		c.Ctx.SetCookie("automatic_login", cooikes, 86400, "/")
	}

	o := orm.NewOrm()
	user_admin := models.AdminUser{Id: id}
	err := o.Read(&user_admin)
	if err == orm.ErrNoRows {
		fmt.Println("查询不到")
		//msg = "用户名密码错误"
	} else if err == orm.ErrMissPK {
		fmt.Println("找不到主键")
		//msg = "找不到主键"
	}
	//查询系统设置信息
	systemData, _ := c.GetSystemSetingData()
	c.Data["user_admin"] = user_admin
	c.Data["system_data"] = systemData
	c.TplName = "admin/index.html"
}

// 后台主页
type AdminMainController struct {
	greenbee.AdminBaseController
}

func (c *AdminMainController) Get() {
	//查询系统设置信息
	systemData, _ := c.GetSystemSetingData()
	c.Data["system_data"] = systemData
	c.TplName = "admin/main/main.html"
}

// 管理员基本资料
type AdminUserSettingController struct {
	greenbee.AdminBaseController
}

func (c *AdminUserSettingController) Get() {
	user := c.GetSession("user")
	id := 0
	if user != nil {
		AdminUser := user.(models.AdminUser)
		id = AdminUser.Id
	}

	o := orm.NewOrm()
	user_admin := models.AdminUser{Id: id}
	err := o.Read(&user_admin)
	if err == orm.ErrNoRows {
		fmt.Println("查询不到")
		//msg = "用户名密码错误"
	} else if err == orm.ErrMissPK {
		fmt.Println("找不到主键")
		//msg = "找不到主键"
	}

	c.Data["user_admin"] = user_admin
	c.TplName = "admin/main/user-setting.html"
}

func (c *AdminUserSettingController) Post() {
	var data interface{}
	id := c.GetString("id", "")
	nickname := c.GetString("nickname", "")
	email := c.GetString("email", "")
	avatar := c.GetString("img_upload", "")
	if id == "" || email == "" {
		c.ErrorJson(500, "参数有误", 0, data)
	}
	//更新用户数据
	uid, _ := strconv.Atoi(id)
	o := orm.NewOrm()
	user := models.AdminUser{Id: uid}
	if o.Read(&user) == nil {
		user.Nickname = nickname
		user.Avatar = avatar
		user.Email = email
		if count, err := o.Update(&user); err == nil {
			c.SuccessJson(200, data, int(count))
		}
	}
	c.ErrorJson(500, "保存失败", 0, data)
	//fmt.Println(id, nickname, email, avatar)

}

// 管理员 修改密码
type AdminUserPasswordController struct {
	greenbee.AdminBaseController
}

func (c *AdminUserPasswordController) Get() {
	user := c.GetSession("user")
	id := 0
	if user != nil {
		AdminUser := user.(models.AdminUser)
		id = AdminUser.Id
	}
	c.Data["user_id"] = id
	c.TplName = "admin/main/user-password.html"
}

func (c *AdminUserPasswordController) Post() {
	var data interface{}
	id := c.GetString("id", "")
	old_password := c.GetString("old_password", "")
	new_password := c.GetString("new_password", "")
	again_password := c.GetString("again_password", "")
	if old_password == "" || new_password == "" || again_password == "" {
		c.ErrorJson(500, "参数有误", 0, data)
	}
	if new_password != again_password {
		c.ErrorJson(500, "两次输入的密码不一致", 0, data)
	}
	//更新用户数据
	uid, _ := strconv.Atoi(id)
	o := orm.NewOrm()
	user := models.AdminUser{Id: uid}
	if o.Read(&user) == nil {
		verify_old_pass := greenbeeUtils.Sha1(greenbeeUtils.Md5(old_password))
		if verify_old_pass != user.Password {
			c.ErrorJson(500, "旧密码错误！", 0, data)
		}
		//修改密码
		user.Password = greenbeeUtils.Sha1(greenbeeUtils.Md5(new_password))
		if count, err := o.Update(&user); err == nil {
			c.SuccessJson(200, data, int(count))
		}
	}
	c.ErrorJson(500, "修改失败", 0, data)

}

// 后台清楚缓存 struct
type AdminClearController struct {
	greenbee.AdminBaseController
}

func (c *AdminClearController) Get() {
	user := c.GetSession("user")
	id := 0
	var data interface{}
	if user != nil {
		AdminUser := user.(models.AdminUser)
		id = AdminUser.Id
		//删除 权限节点 session
		c.DelSession("userAuthorizationUrls")
		var adminUserService adminService.AdminUserService
		authorizationUrls := adminUserService.GetUserAuthorizationUrls(id)
		//更新权限节点 session
		c.SetSession("userAuthorizationUrls", authorizationUrls)
		c.SuccessJson(1, data, 0)
	}

	c.ErrorJson(500, "清理失败", 0, data)
}
